/**
 * 全屏显示封装
 *
 * 全屏面板的优先级过高，
 *
 * 已知问题，如果全屏局部 dom 元素，并且内部组件调整过 z-index，
 * 一般会悬浮在面板后面，这没什么完美的解决手段。
 *
 * 在老版本的浏览器中，不同浏览器，全屏的写法各不相同，
 *
 * @author Mr.css
 */

// 基础全屏接口实现
const StandardScreen = {
    isFullscreen() {
        return !!document.fullscreenElement;
    },

    requestFullscreen(element = document.documentElement) {
        return element.requestFullscreen();
    },

    exitFullscreen() {
        if (this.isFullscreen()) {
            return document.exitFullscreen();
        }
    }
};

const WebkitScreen = {
    isFullscreen() {
        return !!document.webkitFullscreenElement;
    },

    requestFullscreen(element = document.documentElement) {
        return element.webkitRequestFullscreen();
    },

    exitFullscreen() {
        if (this.isFullscreen()) {
            return document.webkitExitFullscreen();
        }
    }
};

const MozScreen = {
    isFullscreen() {
        return !!document.mozFullScreenElement;
    },

    requestFullscreen(element = document.documentElement) {
        return element.mozRequestFullScreen();
    },

    exitFullscreen() {
        if (this.isFullscreen()) {
            return document.mozCancelFullScreen();
        }
    }
};

const MsScreen = {
    isFullscreen() {
        return !!document.msFullscreenElement;
    },

    requestFullscreen(element = document.documentElement) {
        return element.msRequestFullscreen();
    },

    exitFullscreen() {
        if (this.isFullscreen()) {
            return document.msExitFullscreen();
        }
    }
};

// 检测并返回合适的全屏实现
function getScreenImplementation() {
    if (document.documentElement.requestFullscreen) {
        return StandardScreen;
    } else if (document.documentElement.webkitRequestFullscreen) {
        return WebkitScreen;
    } else if (document.documentElement.mozRequestFullScreen) {
        return MozScreen;
    } else if (document.documentElement.msRequestFullscreen) {
        return MsScreen;
    }

    console.error("unknown fullscreen method, maybe cause any exception!");
    return null;
}

const screen = getScreenImplementation();
export default screen;